package com.xgkj.diyiketang.activity.data;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.xgkj.diyiketang.R;
import com.xgkj.diyiketang.activity.YuanwangDetailActivity;
import com.xgkj.diyiketang.data.DesireInfo;
import com.xgkj.diyiketang.util.ImageCache;
import com.xgkj.diyiketang.util.ViewUtil;
import com.xgkj.diyiketang.weidget.RoundImageView;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by windo on 17/1/3.
 */

public class YuanwangListAdapter extends BaseAdapter{
    private List<DesireInfo> mList = new ArrayList<>();

    private Context mContext;
    private LayoutInflater layoutInflater;

    public YuanwangListAdapter(Context context) {
        mContext = context;
        this.layoutInflater = LayoutInflater.from(mContext);
    }

    public void updateData(List<DesireInfo> list){
        mList.addAll(list);
        notifyDataSetChanged();
    }

    public void clearData(){
        mList.clear();
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.yuanwanglist_item, null);
            convertView.setTag(new ViewHolder(convertView));
        }
        final DesireInfo info = mList.get(position);
        initializeViews(info, (ViewHolder) convertView.getTag());
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, YuanwangDetailActivity.class);
                intent.putExtra("ID",info.getDesireId());
                mContext.startActivity(intent);
            }
        });
        return convertView;
    }

    private void initializeViews(DesireInfo info, ViewHolder holder) {
        if (info != null){
            String url = info.getUserLogo();
            if (url != null && url.length()>0){
                ViewUtil.Size size = new ViewUtil.Size(ViewUtil.dip2px(mContext,40),ViewUtil.dip2px(mContext,40));
                String key = ImageCache.getInstance(mContext).getCacheKey(url,size);
                Bitmap cacheBitmap = ImageCache.getInstance(mContext).getBitmapFromMemoryCache(key);
                if (cacheBitmap != null && !cacheBitmap.isRecycled()){
                    holder.headImage.setImageBitmap(cacheBitmap);
                }else {
                    holder.headImage.setImageResource(R.drawable.default_head_image);
                    ImageCache.getInstance(mContext).loadBitmapToView(holder.headImage, url, size);
                }
            }

            try {
                if (!ViewUtil.isNull(info.getNickName())) {
                    String nick = URLDecoder.decode(info.getNickName(), "utf-8");
                    holder.nickname.setText(nick);
                }
                if (!ViewUtil.isNull(info.getDesireTitle())) {
                    String desc = URLDecoder.decode(info.getDesireTitle(), "utf-8");
                    holder.yuanwangDesc.setText(desc);
                }

            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            int status = info.getStatus();
            String yuanwang_status = "";
            if (status == 0){
                yuanwang_status = "未完成";
            }else if (status ==1){
                yuanwang_status = "完成";
            }else if (status ==2){
                yuanwang_status = "作废";
            }
            holder.yuanwangStatus.setText(yuanwang_status);
            holder.createTime.setText(info.getCreateTime());
        }

    }

    protected class ViewHolder {
        private RoundImageView headImage;
        private TextView nickname;
        private TextView createTime;
        private TextView yuanwangStatus;
        private TextView yuanwangDesc;

        public ViewHolder(View view) {
            headImage = (RoundImageView) view.findViewById(R.id.head_image);
            nickname = (TextView) view.findViewById(R.id.nickname);
            createTime = (TextView) view.findViewById(R.id.create_time);
            yuanwangStatus = (TextView) view.findViewById(R.id.yuanwang_status);
            yuanwangDesc = (TextView) view.findViewById(R.id.yuanwang_desc);
        }
    }
}
